package com.zllkj.mm.module.freetime

import android.view.View
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.zllkj.mm.R
import com.zllkj.mm.adapter.CollectionListAdapter
import com.zllkj.mm.adapter.FreeTimeWaitToConfirmListAdapter
import com.zllkj.mm.adapter.WaitToConfirmFreetimeDetailAdapter
import com.zllkj.mm.api.network.RetrofitHelper
import com.zllkj.mm.api.network.RxHelper
import com.zllkj.mm.api.network.subscriber.ProgressSubscriber
import com.zllkj.mm.base.BaseFragment
import com.zllkj.mm.base.BaseTitleFragment
import com.zllkj.mm.common.L
import com.zllkj.mm.model.entity.*
import com.zllkj.mm.module.goods.GoodsDetailFragment
import com.zllkj.mm.module.goods.vm.ItemCollectionsVM
import com.zllkj.mm.module.home.fragments.MyCollectionFragment
import com.zllkj.mm.module.shopping.fragment.OrderListItemFragment
import com.zllkj.mm.util.common.FormatUtil
import com.zllkj.mm.util.common.PictureLoadUtil
import kotlinx.android.synthetic.main.fragment_friend_detail.view.*
import kotlinx.android.synthetic.main.fragment_mycollection.view.*
import java.util.HashMap

class WaitToConfirmFreeTimeOrderDetailFragment(val dyId:WaitToConfirmOrderVoRecord):BaseTitleFragment() {
    var datas = ArrayList<InvitationListVoRecord>()
    lateinit var adapter : WaitToConfirmFreetimeDetailAdapter
    var currentPage = 1



    override fun loadData() {
        super.loadData()

        setCommonTitle("我的闲时")

        adapter = WaitToConfirmFreetimeDetailAdapter(R.layout.item_wait_to_confirm_invitation_data,datas)
        mRootView.rv_mycollection.layoutManager = LinearLayoutManager(requireContext())
        mRootView.rv_mycollection.adapter = adapter


        adapter.setOnItemClickListener { baseQuickAdapter, view, i ->

        }


        mRootView?.smartRefreshlayout_mycollection?.setOnRefreshListener {
            datas.clear()
            currentPage = 1
            loadNetWorkData(OrderListItemFragment.PULL_DOWN)
        }

        mRootView?.smartRefreshlayout_mycollection?.setOnLoadMoreListener {
            currentPage += 1
            loadNetWorkData(OrderListItemFragment.LOAD_MORE)
        }
        loadNetWorkData(-1)

        dyId?.let {
            var ivHead = mRootView.ivhead
            mRootView.tvUserName.text = it.nikeName
            mRootView.tv_item_friend_content.text = it.content
            var address = "|" + it.province + it.city + it.district
            mRootView.tv_age_and_sex.text = if(it.sex == 1)"男" else "女"
            mRootView.tv_age_and_sex.append(address)
            PictureLoadUtil.loadPic(ivHead,it.userLogo)

            if (it.dynamicType == 1) {
                it.price?.let { price ->
                    mRootView.ll_invite_time_area.visibility = View.VISIBLE
                    mRootView.tv_friend_invite_money.text = FormatUtil.bigDecimalUtilFormat(price.toString(), "100") + "/次"
                    mRootView.tv_friend_invite_time.text = FormatUtil.dataFormat(it.createTime)
                }
            }
        }
        adapter.setOnItemChildClickListener { baseQuickAdapter, view, i ->
            if (view.id == R.id.btn_cancel) {
                acceptInvitation(datas[i].freeTimeInvitationId,i)
            } else if (view.id == R.id.btn_confirm){
                acceptInvitation(datas[i].freeTimeInvitationId,i)
            }
        }

    }

    fun acceptInvitation(id:Int,position:Int){
            addSubscribe(RetrofitHelper.getInstance().freeTimeApi.confirmInvitation((HashMap<String, Any>().apply {
                this["freeTimeInvitationId"] = id
            })).compose<String>(RxHelper.defaultHandleResult<String>()).subscribe(object : ProgressSubscriber<String>(requireContext()){
                override fun onSucc(t: String?) {
                    datas.removeAt(position)
                    adapter.notifyDataSetChanged()
                    L.showToast("确认成功")
                }

                override fun onError(e: Throwable?) {
                    super.onError(e)
                }

            }))

    }

    fun cancelInvitation(id:Int,position: Int){
        addSubscribe(RetrofitHelper.getInstance().freeTimeApi.rejectInvitation((HashMap<String, Any>().apply {
            this["freeTimeInvitationId"] = id
        })).compose<String>(RxHelper.defaultHandleResult<String>()).subscribe(object : ProgressSubscriber<String>(requireContext()){
            override fun onSucc(t: String?) {
                datas.removeAt(position)
                adapter.notifyDataSetChanged()
                L.showToast("取消成功")
            }

            override fun onError(e: Throwable?) {
                super.onError(e)
            }

        }))

    }

    override fun getContentLayoutId(): Int {
        return R.layout.fragment_wait_to_confirm_freetime_detail
    }

    fun loadNetWorkData(loadStatus:Int){
        addSubscribe(RetrofitHelper.getInstance().freeTimeApi.getInvitationList((HashMap<String, Any>().apply {
            this["dynamicId"] = dyId.dynamicId
            this["currentPage"] = currentPage
            this["pageSize"] = 20
            this["status"] = 1
        })).compose<InvitationListVo>(RxHelper.defaultHandleResult<InvitationListVo>()).subscribe(object : ProgressSubscriber<InvitationListVo>(requireContext()){
            override fun onSucc(t: InvitationListVo?) {

                t?.records.let {
                    it?.map {
                       datas.add(it)
                    }

                }

                if(loadStatus == OrderListItemFragment.PULL_DOWN) {
                    mRootView.smartRefreshlayout_mycollection.finishRefresh()
                    adapter.notifyDataSetChanged()
                } else if (loadStatus == OrderListItemFragment.LOAD_MORE) {
                    mRootView.smartRefreshlayout_mycollection.finishLoadMore()
                    adapter.notifyDataSetChanged()
                } else {
                    adapter.notifyDataSetChanged()
                }
            }

            override fun onError(e: Throwable?) {
                super.onError(e)
            }

        }))

    }

}